package com.caterina.activities;

import java.awt.Container;
import java.util.ArrayList;
import java.util.Collection;

import modulesAPI.v2.Module;
import modulesAPI.v2.ModuleChangedListener;
import modulesAPI.v2.ViewType;
import modulesAPI.v2.basics.ModCollectionViewer;
import modulesAPI.v2.composite.MEmployeeContract;

import com.caterina.activityManager.CActivity;
import com.caterina.components.CButton;

import contract.EmployeeContract;
import controllers.ManagerTask.TaskType;

public class EmployeeContractsActivity extends CActivity implements ModuleChangedListener {
	public EmployeeContractsActivity(Container parent) {
		super("EMPLOYEE_CONTRACTS_ACTIVITY",parent);
	}
	@Override
	public void onStart() {
		this.putTask(TaskType.EMPLOYEE_CONTRACT_GET_ALL, "");
	}
	@SuppressWarnings("unchecked")
	@Override
	public void onData(TaskType task, Object data) {
		switch (task){
			case EMPLOYEE_CONTRACT_GET_ALL:{
				this.lastData=(Collection<EmployeeContract>) data;
				this.createView();
				break;
			}
			case EMPLOYEE_CONTRACT_REMOVE:{
				this.startLoading();
				this.putTask(TaskType.EMPLOYEE_CONTRACT_GET_ALL,"");
				break;
			}
		default:
			break;
		}
	}
	
	private void createButtons(){
		this.fires=new CButton("EMPLOYEE_FIRE") {	
			@Override
			public void onFocus() {	}
			@Override
			public void onAction() {
				EmployeeContractsActivity.this.putTask(TaskType.EMPLOYEE_CONTRACT_REMOVE,collectionViewer.getSelectedContent().getContent());
			}
			private static final long serialVersionUID = 1L;
		};
	}
	@Override
	public void onError(TaskType task, Exception e) {}
	@Override
	public void onModuleChange(Module<?> mod) {
		this.add(collectionViewer);
		this.refresh();
	}
	private void createView(){
		ArrayList<Module<?>> cCnts=new ArrayList<Module<?>>();
		this.createButtons();
		for(EmployeeContract eCnt: lastData){
			MEmployeeContract ecnt =new MEmployeeContract(null, eCnt,"EMPLOYEE","EMPLOYEE_DESC", ViewType.READ_ONLY);
			ecnt.addButtons(fires);
			cCnts.add(ecnt);
		}
		this.collectionViewer=new ModCollectionViewer(null,cCnts, "EMPLOYEE", "EMPLOYEE", ViewType.WRITABLE,ModCollectionViewer.HIDE_ADD,ModCollectionViewer.HIDE_REMOVE);
		collectionViewer.addModuleChangedListener(this);
		this.add(collectionViewer);
	}
	private ModCollectionViewer collectionViewer;
	private CButton fires;
	private Collection<EmployeeContract> lastData;
	private static final long serialVersionUID = 1L;
}
